﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        public bool SaveSite(Site site)
        {
            Logger.Info("core", string.Format("begin save site:{0}", site.Id));

            using (CoreDbContext context = new CoreDbContext())
            {
                if (context.Set<Site>()
                    .Where(p => p.Id == site.Id)
                    .Select(p => new { p.Id })
                    .FirstOrDefault() == null)
                {
                    Logger.Info("core", string.Format("site:{0} does not exist in database, excute insert", site.Id));
                    context.Set<Site>().Add(site);
                }
                else
                {
                    Logger.Info("core", string.Format("site:{0} exist in database, excute updae", site.Id));
                    context.Entry(site).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    Logger.Info("core", string.Format("save site:{0} success", site.Id));
                    return true;
                }
                else
                {
                    Logger.Error("core", string.Format("save site:{0} failure", site.Id));
                    return false;
                }
            }
        }

        public Site GetSite(Guid id)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<Site>()
                    .Where(p => p.Id == id)
                    .FirstOrDefault();
            }
        }

        public Site GetSite(string hostUrl)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<Site>()
                    .Join(context.SiteHostSet.Where(p=>p.Host == hostUrl),
                    left=>left.Id,
                    right=>right.SiteId,
                    (left,right)=>left).SingleOrDefault();
            }
        }

        public bool DeleteSite(Guid siteId)
        {
            Logger.Info("core", string.Format("begin delete site:{0}", siteId));

            using (CoreDbContext context = new CoreDbContext())
            {
                Site application = context.Set<Site>().Where(p => p.Id == siteId).SingleOrDefault();
                if (application != null)
                {
                    context.Entry(application).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        Logger.Info("core", string.Format("delete site:{0} success", siteId));
                        return true;
                    }
                }
                else
                {
                    Logger.Info("core", string.Format("delete site:{0} success, this site does not exist in the database", siteId));
                    return true;
                }
            }
            Logger.Info("core", string.Format("delete application:{0} failure", siteId));
            return false;
        }

        public List<Site> GetSites()
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<Site>()
                    .OrderBy(p => p.SortIndex).ToList();
            }
        }

        public List<Site> GetSites(string type)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<Site>()
                    .Where(p => p.Type == type)
                    .OrderBy(p => p.SortIndex).ToList();
            }
        }
    }
}
